package com.sunny.custom.spring.servlet.mvc.web;

import com.sunny.custom.spring.servlet.framework.anno.CustomAutowired;
import com.sunny.custom.spring.servlet.framework.anno.CustomController;
import com.sunny.custom.spring.servlet.framework.anno.CustomRequestMapping;
import com.sunny.custom.spring.servlet.framework.anno.CustomRequestParam;
import com.sunny.custom.spring.servlet.mvc.service.IDemoService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 测试自定义Spring
 *
 * @author Sunny
 * @date 2020/1/16
 */
@CustomController
@CustomRequestMapping("/custom")
public class CustomControllerTest {

    @CustomAutowired
    private IDemoService demoService;

    @CustomRequestMapping("/query")
    public void query(HttpServletResponse response, @CustomRequestParam("name") String name) {
        try {
            response.getWriter().write(demoService.get(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CustomRequestMapping("/add")
    public void add(HttpServletResponse response, @CustomRequestParam("a") int a, @CustomRequestParam("b") int b) {
        try {
            response.getWriter().write(a + " + " + b + " = " + (a + b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CustomRequestMapping("/minus")
    public void minus(HttpServletResponse response, @CustomRequestParam("a") double a, @CustomRequestParam("b") double b) {
        try {
            response.getWriter().write(a + " - " + b + " = " + (a - b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CustomRequestMapping("/remove")
    public String remove(@CustomRequestParam("id") int id) {
        return id + "";
    }

    @CustomRequestMapping("/regex.*")
    public String regex() {
        return "正则测试成功";
    }
}
